package com.wanpinghui.supplier.user;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.user.adapter.CreditRuleAdapter;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.wanpinghui.wphlibrary.model.oauth.UserInfoUtil;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserInformation;
import com.wanpinghui.wphlibrary.model.user.UserInfoService;
import com.wanpinghui.wphlibrary.model.user.bean.CreditRuleBean;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit.callback.YRequestCallback;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/11/23
 */
@ContentView(R.layout.layout_activity_member_level)
public class MemberLevelActivity extends BaseActivity implements YRequestCallback<List<CreditRuleBean>> {

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser mLayoutTitle;

    @ViewInject(R.id.tv_level)
    private TextView tv_level;

    @ViewInject(R.id.tv_tisheng)
    private TextView tv_tisheng;

    @ViewInject(R.id.ll_tisheng)
    private LinearLayout ll_tisheng;

    @ViewInject(R.id.ll_level_content)
    private LinearLayout ll_level_content;

    @ViewInject(R.id.tv_pass_cnt_percent)
    private TextView tv_pass_cnt_percent;

    @ViewInject(R.id.ll_chengjiu)
    private LinearLayout ll_chengjiu;

    @ViewInject(R.id.tv_credit)
    private TextView tv_credit;

    @ViewInject(R.id.tv_cjz)
    private TextView tv_cjz;

    @ViewInject(R.id.tv_level_type)
    private TextView tv_level_type;

    @ViewInject(R.id.iv_level)
    private ImageView iv_level;

    @ViewInject(R.id.tv_level_2)
    private TextView tvLevel2;

    @ViewInject(R.id.tv_level_2_content)
    private TextView tvLevel2Content;

    @ViewInject(R.id.tv_level_3)
    private TextView tvLevel3;

    @ViewInject(R.id.tv_level_3_content)
    private TextView tvLevel3Content;

    @ViewInject(R.id.tv_level_4)
    private TextView tvLevel4;

    @ViewInject(R.id.tv_level_4_content)
    private TextView tvLevel4Content;

    @ViewInject(R.id.iv_line2)
    private ImageView iv_line2;

    @ViewInject(R.id.iv_line3)
    private ImageView iv_line3;

    @ViewInject(R.id.recyclerview)
    private RecyclerView recyclerView;

    private CreditRuleAdapter adapter;


    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        tv_tisheng.setSelected(true);
    }

    @Override
    public void bindEvents() {
        adapter = new CreditRuleAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        UserBean userBeanInfo = UserInfoUtil.getUserInfo();
        showInfo(userBeanInfo);
        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_level.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  showWarningText("点击了等级说明");
                tv_level.setSelected(true);
                tv_cjz.setSelected(false);
                tv_tisheng.setSelected(false);
                recyclerView.setVisibility(View.GONE);
                ll_chengjiu.setVisibility(View.GONE);
                ll_level_content.setVisibility(View.VISIBLE);
            }
        });
        tv_tisheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_level.setSelected(false);
                tv_cjz.setSelected(false);
                tv_tisheng.setSelected(true);
                recyclerView.setVisibility(View.VISIBLE);
                ll_chengjiu.setVisibility(View.GONE);
                ll_level_content.setVisibility(View.GONE);
            }
        });
        tv_cjz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_cjz.setSelected(true);
                tv_level.setSelected(false);
                tv_tisheng.setSelected(false);
                ll_level_content.setVisibility(View.GONE);
                recyclerView.setVisibility(View.GONE);
                ll_chengjiu.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void initData() {
        WPHSdk.getService(UserInfoService.class).credit_rule(this);
        //getUserInformation();
    }

    private void getUserInformation() {
        WPHSdk.getService(OAuthService.class)
                .getUserInfo(new YRequestCallback<UserBean>() {
                    @Override
                    public void onSuccess(UserBean userBeanInfo) {
                        showInfo(userBeanInfo);
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        //   showWarningText("登录失败，获取用户信息失败");
                    }

                    @Override
                    public void onException(Throwable var1) {
                        var1.printStackTrace();
                    }
                });
    }

    public void showInfo(UserBean userBeanInfo) {
        if (userBeanInfo != null && userBeanInfo.getUser() != null) {
            UserInformation userBean = userBeanInfo.getUser();
            if (userBean.getCredit() > 10000) {
                tv_credit.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30); //设置45SP
                tv_credit.setText(String.valueOf(userBean.getCredit()));
            } else {
                tv_credit.setText(String.valueOf(userBean.getCredit()));
            }
            if (userBean.getLevel() <= 10) {
                tv_pass_cnt_percent.setText("您已超越" + userBean.getPass_cnt() + "人，击败了" + userBean.getPass_percent() + "%的铁牌会员");
                tv_level_type.setText("铁牌会员");
            } else {
                tv_level_type.setText(userBean.getLevel_display() + "会员");
                tv_pass_cnt_percent.setText("您已超越" + userBean.getPass_cnt() + "人，击败了" + userBean.getPass_percent() + "%的" + userBean.getLevel_display() + "会员");
            }

            switch (userBean.getLevel()) {
                case 10:
                    iv_level.setImageDrawable(ContextCompat.getDrawable(MemberLevelActivity.this, R.mipmap.my_icon_member_1));
                    break;
                case 20:
                    tvLevel2.setSelected(true);
                    tvLevel2Content.setSelected(true);
                    iv_line2.setImageDrawable(ContextCompat.getDrawable(MemberLevelActivity.this, R.mipmap.my_icon_class_line_s));
                    iv_level.setImageDrawable(ContextCompat.getDrawable(MemberLevelActivity.this, R.mipmap.my_icon_member_2));
                    break;
                case 30:
                    tvLevel2.setSelected(true);
                    tvLevel2Content.setSelected(true);
                    tvLevel3.setSelected(true);
                    tvLevel3Content.setSelected(true);
                    iv_line2.setImageDrawable(ContextCompat.getDrawable(MemberLevelActivity.this, R.mipmap.my_icon_class_line_s));
                    iv_line3.setImageDrawable(ContextCompat.getDrawable(MemberLevelActivity.this, R.mipmap.my_icon_class_line_s));
                    iv_level.setImageDrawable(ContextCompat.getDrawable(MemberLevelActivity.this, R.mipmap.my_icon_member_3));
                    break;
                case 40:
                    tvLevel2.setSelected(true);
                    tvLevel2Content.setSelected(true);
                    tvLevel3.setSelected(true);
                    tvLevel3Content.setSelected(true);
                    tvLevel4.setSelected(true);
                    iv_line2.setImageDrawable(ContextCompat.getDrawable(MemberLevelActivity.this, R.mipmap.my_icon_class_line_s));
                    iv_line3.setImageDrawable(ContextCompat.getDrawable(MemberLevelActivity.this, R.mipmap.my_icon_class_line_s));
                    tvLevel4Content.setSelected(true);
                    iv_level.setImageDrawable(ContextCompat.getDrawable(MemberLevelActivity.this, R.mipmap.my_icon_member_4));
                    break;
            }
        }
    }

    @Override
    public void onSuccess(List<CreditRuleBean> var1) {
        adapter.setDatas(var1);
    }

    @Override
    public void onFailed(int var1, String message) {
        showWarningText(message);
    }

    @Override
    public void onException(Throwable var1) {
        var1.printStackTrace();
    }
}
